package com.leanyuan.check.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.check.domain.Conditiontask;
import com.leanyuan.check.domain.Positionplan;
import com.leanyuan.check.dto.Positionplandto;
import com.leanyuan.check.service.ConditiontaskService_C;
import com.leanyuan.check.service.PositionplanService_C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;


@RestController
@RequestMapping("/leanyuan/Positionplan")
public class PositionplanController {

    @Autowired
    PositionplanService_C positionplanServiceC;
    @Autowired
    ConditiontaskService_C conditiontaskServiceC;

    //分页查询
    @RequestMapping("FindAllPositionplan")
    public Page<Positionplandto> FindAllPositionplan(int pagesize, int pageno, Model model){
        System.out.println("巡检计划");
        Page<Positionplandto> list= positionplanServiceC.InspectiondtolistPage(pageno,pagesize);
        model.addAttribute("Positionplanlist",list);
        return list;
    }
    //分页查询
    @RequestMapping("FindAllPositionplan1")
    public Page<Positionplandto> FindAllPositionplan1(int pagesize, int pageno, String id, String name, String plancondition, String begintime , String endtime, Model model) throws ParseException {
        Positionplandto positionplandto=new Positionplandto();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy ", Locale.ENGLISH);
        if(begintime!=null&&begintime!="") {
            Date aa = sdf.parse(begintime); //将字符串改为date的格式
            positionplandto.setBegintime(aa);
        }
        if(endtime!=null&&endtime!="") {
            Date bb = sdf.parse(endtime);positionplandto.setEndtime(bb);
        }
        if(id!=null&&id!="")positionplandto.setPositionplan_id(Integer.parseInt(id));
        else positionplandto.setPositionplan_id(null);
        positionplandto.setPositionplan_name(name);
        positionplandto.setPlancondition(plancondition);
        System.out.println(positionplandto);
        Page<Positionplandto> list= positionplanServiceC.InspectiondtolistPage1(pageno,pagesize,positionplandto);
        model.addAttribute("Positionplanlist",list);
        return list;
    }

    @RequestMapping("FindAllPositionplan2")
    public List<Positionplan> FindAllPositionplan2(int pagesize, int pageno, Model model){
        System.out.println("巡检任务");
        List<Conditiontask> tasklist= conditiontaskServiceC.list(null);
        QueryWrapper wrapper=new QueryWrapper();
        for (Conditiontask c:tasklist) {
            wrapper.notIn("positionplan_id", c.getPositionplanId());
        }
        wrapper.eq("plancondition","启用");
        List<Positionplan> list= positionplanServiceC.list(wrapper);
        model.addAttribute("Positionplanlist",list);
        return list;
    }
    //添加任务
    @RequestMapping("SavePositionplan")
    public Positionplan SavePositionplan(@RequestBody Positionplan positionplan){
        System.out.println(positionplan);
        boolean flag= positionplanServiceC.save(positionplan);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("begintime",positionplan.getBegintime());
        wrapper.eq("endtime",positionplan.getEndtime());
        wrapper.eq("name",positionplan.getName());
        wrapper.eq("Fashion",positionplan.getFashion());
        Positionplan p= positionplanServiceC.getOne(wrapper);
        return positionplan;
    }
    //修改任务
    @RequestMapping("UpdatePositionplan")
    public String UpdatePositionplan(@RequestBody Positionplan positionplan){
        QueryWrapper<Positionplan> queryWrapper=new QueryWrapper<Positionplan>();
        queryWrapper.eq("Position_id",positionplan.getPositionplanId());
        boolean flag= positionplanServiceC.update(positionplan,queryWrapper);
        if(flag)return "修改成功";
        return "修改失败";
    }
    //删除任务
    @RequestMapping("DeletePositionplan")
    public String DeletePositionplan(Integer id){
        QueryWrapper<Positionplan> queryWrapper=new QueryWrapper<Positionplan>();
        queryWrapper.eq("positionplan_id",id);
        Positionplan positionplan= positionplanServiceC.getOne(queryWrapper);
        System.out.println(positionplan);
        positionplan.setPlancondition("停用");
        boolean flag= positionplanServiceC.update(positionplan,queryWrapper);
        if(flag)return "删除成功";
        return "删除失败";
    }

    @RequestMapping("DeletePositionplan1")
    public String DeletePositionplan1(Integer id){
        QueryWrapper<Positionplan> queryWrapper=new QueryWrapper<Positionplan>();
        queryWrapper.eq("positionplan_id",id);
        Positionplan positionplan= positionplanServiceC.getOne(queryWrapper);
        System.out.println(positionplan);
        positionplan.setPlancondition("启用");
        boolean flag= positionplanServiceC.update(positionplan,queryWrapper);
        if(flag)return "删除成功";
        return "删除失败";
    }
}
